extends PanelContainer

@onready var bag = $"../.."
@onready var ingredient1 = $"../../workplace/Panel/ingredient/ingredient1"
@onready var ingredient2 = $"../../workplace/Panel/ingredient/ingredient2"
@onready var ingredient3 = $"../../workplace/Panel/ingredient/ingredient3"





# Called when the node enters the scene tree for the first time.
func _ready():
	mouse_filter = Control.MOUSE_FILTER_PASS
	gui_input.connect(_on_gui_input)


func _on_gui_input(event) -> void:
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
			print("尝试合成！",GameUtiles.ingredients)
			var ingredients = GameUtiles.ingredients
			var sorted_ingredients = ingredients.duplicate()
			sorted_ingredients.sort()
			if sorted_ingredients == []:
				print("空！")
				
			if GameUtiles.recipes.has(sorted_ingredients) :
				
				#判断是否达到上限
				var enough = false
				for item in GameUtiles.recipes[sorted_ingredients]:
					if GameUtiles.player_bag.get(item,0)>=9:
						enough = true
				
				if not enough:
					print("成功合成",GameUtiles.recipes[sorted_ingredients])
					#获取产物
					var output = GameUtiles.recipes[sorted_ingredients]
					for item in output:
						GameUtiles.add_item(item)
						bag.fill_in_bag()
					#移除合成槽中的物品
					while GameUtiles.ingredients != []:
						for item in GameUtiles.ingredients:
							if ingredient1.item == item:
								ingredient1.clear_ingredient_sprite()
								GameUtiles.ingredients.erase(item)
							if ingredient2.item == item:
								ingredient2.clear_ingredient_sprite()
								GameUtiles.ingredients.erase(item)
							if ingredient3.item == item:
								ingredient3.clear_ingredient_sprite()
								GameUtiles.ingredients.erase(item)
				else:
					DialogueManager.show_dialogue_balloon(load("res://dialogues/enough.dialogue"),"start")
				sorted_ingredients = []
					
					
